// pages/clerkOrderList/clearkOrderList.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index1: 0,
    option1: ['待付款', '已付款', '已完成', '已收货', '已评价'],
    src: '',//二维码图片地址
    image: '',
    showModel: false,
    list: [],
    baseImgPath: app.globalData.baseImgPath,
    showPjInput: false,//显示评价提交框
    currentTab: "order",
    current: "2",
    page:1,
    qrcode:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
   // console.log(options.qrcode)
    console.log(options.current)
    if (options.current!=undefined){
      that.setData({
        current: options.current
      })
    }
   
    wx.getStorage({
      key: 'sessionId',
      success: function (res) {
        that.setData({
          sessionId: res.data, //登陆页面取回来的sessionId
        });
        console.log(that.data.sessionId);
        wx.request({
          url: app.globalData.urlPath + '/ManagerMent/masterList',
          data: {
            sessionid: that.data.sessionId,
            id: that.data.id,
            status: that.data.current,
            page:that.data.page
          },
          method: "POST",
          success: function (res) {
            console.log(res)
            if (res.data.code == 200) {
              var data = res.data.data;
              that.setData({
                list: data
              })
            }
            else {
              // wx.showToast({
              //   title: res.data.error,
              //   icon: 'none',
              //   duration: 2000,
              //   mask: true
              // })
            }
          }
        })
      },
    })
  },
  //底部导航切换
  handleChangeTab({ detail }) {
    var that =this;
    console.log(detail.key)
    if (detail.key == 'order') {
      wx.redirectTo({
        url: '/pages/masterOrderList/masterOrderList',
      })
    } else if (detail.key == 'code') {
      // 只允许从相机扫码
      // wx.scanCode({
      //   onlyFromCamera: true,
      //   success(res) {
      //     console.log(res)
      //   }
      // }) // 只允许从相机扫码
      wx.scanCode({
        onlyFromCamera: true,
        success(res) {
          console.log(res);
          that.setData({
            qrcode: res.result
          })
          //加载二维码返回数据
          wx.request({
            url: app.globalData.urlPath + '/ManagerMent/scanqrcode',
            data: {
              sessionid: app.globalData.sessionid,
              qrcode: that.data.qrcode
            },
            method: "POST",
            success: function (res) {
              console.log(res)
              if (res.data.code == 200) {
                that.setData({
                  orderInfo: res.data.data,
                  fbrand: res.data.data.fbrand,
                  fmaterial: res.data.data.fmaterial,
                  fsize: res.data.data.fsize,
                })
                wx.redirectTo({
                  url: '../masterSubOrder/masterSubOrder?qrcode=' + that.data.qrcode,
                })
              }
              else {
                wx.showToast({
                  title: res.data.error,
                  icon: 'none',
                  duration: 2000
                })
                if (res.data.code == 5000) {
                  wx, wx.redirectTo({
                    url: '/pages/login/login',
                  })
                }
              }
            }
          })

        }
      })
    } else if (detail.key == 'my') {
      wx.redirectTo({
        url: '/pages/masterMy/masterMy',
      })
    }
    this.setData({
      currentTab: detail.key
    });
  },
  //导航切换
  handleChange({ detail }) {
    var that = this;
    this.setData({
      current: detail.key
    });
    console.log(detail)
    //根据传的status查询对应的列表
    wx.request({
      url: app.globalData.urlPath + 'ManagerMent/masterList',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      data: {
        sessionid: that.data.sessionId,
        status: detail.key,
        page:1
      },
      method: "POST",
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            list: res.data.data
          })
        }
        if (res.data.code == 400) {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000,
            mask: true
          })
          that.setData({
            list: []
          })
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let self = this;
    self.data.page += 1;
    wx.request({
      url: app.globalData.urlPath + '/ManagerMent/masterList',
      data: {
        sessionid: self.data.sessionId,
        id: self.data.id,
        status: self.data.current,
        page: self.data.page
      },
      method: "POST",
      success: function (res) {
        console.log(res.data.data)
        let list = res.data.data;
        if (res.data.code == 400) {
          self.setData({ showend: true });
          return false
        }
        self.setData({ list: self.data.list.concat(list) })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
//开始服务
  linkToStartService:function(e){
    var id=e.target.id;
    wx.redirectTo({
      url: '/pages/masterStartService/masterStartService?id='+id,
    })
  }
})